package com.thumbnail;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
// import org.springframework.web.portlet.ModelAndView; 
// 导包如果错误，前台jsp页面取不到值！！！！
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class ThumbnailAction {
	
	private UploadService uploadService;
	private ThumbnailService thumbnailService;
	
	@RequestMapping(value="/thumbnail", method=RequestMethod.POST)
	public ModelAndView thumbnail(@RequestParam("image")CommonsMultipartFile file, HttpSession session)throws Exception {
		String uploadPath = "/images"; //上传路径，也就是相对路径
		String realUploadPath = session.getServletContext().getRealPath(uploadPath); //真实的路径，也就是绝对路径
		System.out.println("真实的路径: " + realUploadPath);
		
		String imageUrl = uploadService.uploadImage(file, uploadPath, realUploadPath); //原图片路径
		System.out.println("原图片路径: " + imageUrl);
		String thumbImageUrl = thumbnailService.thumbnail(file, uploadPath, realUploadPath); //缩略图路径
		System.out.println("缩略图路径: " + thumbImageUrl);
		
		ModelAndView ret = new ModelAndView();
		ret.addObject("imageUrl", imageUrl);
		ret.addObject("thumbImageUrl", thumbImageUrl);
		ret.setViewName("thumbnail");
		
		return ret;
	}

	@Autowired
	public void setUploadService(UploadService uploadService) {
		this.uploadService = uploadService;
	}

	@Autowired
	public void setThumbnailService(ThumbnailService thumbnailService) {
		this.thumbnailService = thumbnailService;
	}
	
	
	
}
